package cn.itcast.solr;

import cn.itcast.pojo.Item;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

/**
 * @author bushifeng
 * @version v1.0
 * @date 2019/2/20 17:12
 * @description TODO
 **/
public class SolrTest {
    //hei
    @Test
    public void test() throws IOException, SolrServerException {
        String url="http://localhost:8080/solr/core2";
        HttpSolrServer solrServer = new HttpSolrServer(url);
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", 15L);
        document.addField("title", "苹果手机");
        document.addField("price", 100000);
        solrServer.add(document);
        solrServer.commit();
    }
    @Test
    public void test1() throws IOException, SolrServerException {
        String url="http://localhost:8080/solr/core2";
        HttpSolrServer solrServer = new HttpSolrServer(url);
        Item item = new Item();
        item.setId(16L);
        item.setTitle("小米手机");
        item.setPrice(10000L);
        solrServer.addBean(item);
        solrServer.commit();
    }
    @Test
    public void testDelete() throws IOException, SolrServerException {
        String url="http://localhost:8080/solr/core2";
        HttpSolrServer solrServer = new HttpSolrServer(url);
        //solrServer.deleteById("16");
        solrServer.deleteByQuery("title:苹果手机");
        solrServer.commit();
    }
    @Test
    public void testResultDocument() throws IOException, SolrServerException {
        String url="http://localhost:8080/solr/core2";
        HttpSolrServer query = new HttpSolrServer(url);
        SolrQuery solrQuery = new SolrQuery("title:Apple");
        QueryResponse response = query.query(solrQuery);
        SolrDocumentList results = response.getResults();
        System.out.println("本次共搜索到"+results.size()+"条数据");
        for (SolrDocument result : results) {
            System.out.println("id"+result.getFieldValue("id"));
            System.out.println("title"+result.getFieldValue("title"));
            System.out.println("price"+result.getFieldValue("price"));
        }
        query.commit();
    }
    @Test
    public void testResultBean() throws SolrServerException {
        HttpSolrServer query = new HttpSolrServer("http://localhost:8080/solr/core2");
        //范围查询[]包含 {}不包含
        SolrQuery solrQuery = new SolrQuery("title:Apple AND price:[1000 TO 2000000]");
        QueryResponse response = query.query(solrQuery);
        List<Item> items = response.getBeans(Item.class);
        System.out.println("本次共搜索到"+items.size()+"条数据");
        items.forEach(System.out::println);
    }
    @Test
    public void testSort() throws SolrServerException {
        HttpSolrServer query = new HttpSolrServer("http://localhost:8080/solr/core2");
        //范围查询[]包含 {}不包含
        SolrQuery solrQuery = new SolrQuery("title:Apple");
        solrQuery.setSort("price", SolrQuery.ORDER.asc);
        QueryResponse response = query.query(solrQuery);
        List<Item> items = response.getBeans(Item.class);
        System.out.println("本次共搜索到"+items.size()+"条数据");
        items.forEach(System.out::println);
    }
    @Test
    public void testPage() throws SolrServerException {
        int pageNum=3;
        int pageSize=2;
        int start=(pageNum-1)*pageSize;

        HttpSolrServer query = new HttpSolrServer("http://localhost:8080/solr/core2");
        //范围查询[]包含 {}不包含
        SolrQuery solrQuery = new SolrQuery("title:手机");
        solrQuery.setSort("price", SolrQuery.ORDER.asc);
        solrQuery.setStart(start);
        solrQuery.setRows(pageSize);
        QueryResponse response = query.query(solrQuery);
        List<Item> items = response.getBeans(Item.class);
        System.out.println("当前第"+pageNum+"页，当前页共"+items.size()+"条数据");
        items.forEach(System.out::println);
    }
    @Test
    public void testHighlight() throws SolrServerException {
        HttpSolrServer query = new HttpSolrServer("http://localhost:8080/solr/core2");
        SolrQuery solrQuery = new SolrQuery("title:Apple");
        //solrQuery.setHighlight(true);
        solrQuery.setHighlightSimplePre("<em>");
        solrQuery.setHighlightSimplePost("</em>");
        solrQuery.addHighlightField("title");

        QueryResponse response = query.query(solrQuery);
        Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();

        List<Item> items = response.getBeans(Item.class);
        items.forEach(item -> {
            item.setTitle(highlighting.get(item.getId().toString()).get("title").get(0));
            System.out.println(item);
        });
    }


}
